package org.tangdao.modules.file.service.impl;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.tangdao.common.io.FileUtils;
import org.tangdao.modules.file.model.domain.FileEntity;
import org.tangdao.modules.file.model.domain.FileUpload;

@Service
public class FileUploadServiceExtend {

	/**
	 * 保存上传文件
	 * @param fileUpload
	 */
	public void saveUploadFile(FileUpload fileUpload) {
	}

	/**
	 * 上传文件
	 * @param fileEntity
	 */
	public void uploadFile(FileEntity fileEntity) {
	}

	/**
	 * 文件对象是否存在
	 * @param fileEntity
	 * @return
	 */
	public boolean fileExists(FileEntity fileEntity) {
		String fileRealPath = fileEntity.getFileRealPath();
		return new File(fileRealPath).exists();
	}

	/**
	 * 文件地址
	 * @param fileUpload
	 * @return
	 */
	public String getFileUrl(FileUpload fileUpload) {
		return fileUpload.getFileEntity().getFileUrl();
	}

	/**
	 * 下载文件
	 * @param fileUpload
	 * @param request
	 * @param response
	 * @return
	 */
	public String downFile(FileUpload fileUpload, HttpServletRequest request, HttpServletResponse response) {
		FileEntity fileEntity = fileUpload.getFileEntity();
		File file = new File(fileEntity.getFileRealPath());
		if (file.exists()) {
			FileUtils.downFile(file, request, response, fileUpload.getFileName());
			return null;
		}
		return "404";
	}
}
